package yxy.game.pm2.module.therionHunting;

import org.joda.time.DateTime;
import org.redisson.api.RList;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @name TherionRecord
 * @author wrs
 * @descrition 神兽挑战记录
 * @since 2022-06-15
 */
public class TherionRecord extends PlayerModule {

    private static int expireDay = 1; //战斗记录过期时间

    // bean===========================================================================
    private DateTime date;// 时间
    private String recordCode;// 录像code

    private int therionId; //神兽ID

    private String attackerCode;// 进攻方
    private String defenderCode;// 防守方

    private BattleResult battleResult;

    public DateTime getDate() {
        return date;
    }

    public void setDate(DateTime date) {
        this.date = date;
    }

    public String getRecordCode() {
        return recordCode;
    }

    public void setRecordCode(String recordCode) {
        this.recordCode = recordCode;
    }

    public String getAttackerCode() {
        return attackerCode;
    }

    public void setAttackerCode(String attackerCode) {
        this.attackerCode = attackerCode;
    }

    public String getDefenderCode() {
        return defenderCode;
    }

    public void setDefenderCode(String defenderCode) {
        this.defenderCode = defenderCode;
    }

    public BattleResult getBattleResult() {
        return battleResult;
    }

    public void setBattleResult(BattleResult battleResult) {
        this.battleResult = battleResult;
    }

    public int getTherionId() {
        return therionId;
    }

    public void setTherionId(int therionId) {
        this.therionId = therionId;
    }

    // func===========================================================================

    private static void create(String playerCode, List<TherionRecord> list) throws RedisException {
        touch(playerCode).addAll(list);
    }

    // redis==========================================================================
    static public String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:THERIONRECORD", playerCode);
    }

    static private RList<TherionRecord> touch(String playerCode) throws RedisException {
        return RedisManager.client(redis()).getList(key(playerCode));
    }

    static public List<TherionRecord> get(String playerCode) {
        try {
            List<TherionRecord> list = touch(playerCode).readAll();
            if (list.size() == 0) {
                List<TherionRecord> records = new ArrayList<>();
                create(playerCode,records);
            }
            //todo 后期存放到数据库
            return list;
        } catch (RedisException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    static public void add(String playerCode, TherionRecord record) {
        try {
            RList<TherionRecord> rlist = touch(playerCode);
            rlist.add(0, record);
            //默认一天的过期
            rlist.expire(expireDay, TimeUnit.DAYS);
            if (rlist.size() > 20) {
                rlist.trim(0, 19);
            }
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    //数据归档到数据库
    public static void saveToDB(String playerCode) {


    }

    //从redis移走数据
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }
    // lock==========================================================================
    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }
}
